package agents;

import java.util.ArrayList;
import java.util.HashMap;
import strategies.Strategy;

public class StrategyHistory {

	protected int stratNo;
	protected ArrayList<Strategy> history;
	
	class histResult{
		Strategy strat;
		double freq;
		
		public histResult(Strategy strat, double freq){
			this.strat = strat;
			this.freq = freq;
		}
	};
	
	public StrategyHistory(int stratNo){
		this.stratNo = stratNo;
		history = new ArrayList<Strategy>(stratNo);
	}
	
	public void put(Strategy strat){
		if (history.size() >= stratNo)
			history.remove(0);
		history.add(strat);
	}
	
	public histResult getMostUsed(){
		HashMap<Strategy, Integer> freq = new HashMap<Strategy, Integer>();
		int maxFreq = 0;
		Strategy mostUsed = null;		
		
		for(Strategy str:history){
			int noHits = (freq.get(str) == null ? 0 : freq.get(str)) + 1;
			freq.put(str,  noHits);
			if (noHits > maxFreq){
				maxFreq = noHits;
				mostUsed = str;
			}
		}
		
		histResult result = new histResult(mostUsed, ((double)maxFreq * 100)/stratNo);
		return result;		
	}
	
	@ Override
	public String toString(){
		histResult res = getMostUsed();
		StringBuilder sb = new StringBuilder();
		
		sb.append(res.strat.toString()).append(" - ").append(res.freq);
		
		return sb.toString();
	}
	
	
}
